﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using PTExpress.DataAccess;
using PTExpress.Templates;

namespace PTExpress.Views
{
    /// <summary>
    /// Interaction logic for frmThemXe.xaml
    /// </summary>
    public partial class frmThemXe : Window
    {
        public bool UpdateSucces = false;
        Command cm;
        DataTable Data = new DataTable();

        public bool IsEditMode
        {
            get { return (bool)GetValue(IsEditModeProperty); }
            set { SetValue(IsEditModeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsEditMode.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsEditModeProperty =
            DependencyProperty.Register("IsEditMode", typeof(bool), typeof(frmThemXe), new UIPropertyMetadata(true));

        public frmThemXe()
        {
            InitializeComponent();
            IsEditMode = true;
        }

        public frmThemXe(DataTable tb, Command _cm)
        {
            InitializeComponent();
            cbbLoaiXe.ItemsSource = LoaiXeAccess.GetAllLoaiXe("").DefaultView;
            cbbTinhTrang.ItemsSource = TinhTrangXeAccess.GetAllTinhTrangXe("").DefaultView;
            cbbLoaiGhe.ItemsSource = LoaiXeAccess.GetAllLoaiXe("").DefaultView;

            IsEditMode = _cm == Command.View ? false : true;
            cm = _cm;
            if (tb == null)
                Data = XeAccess.CreateNewXe();
            else
                Data = tb.Copy();
            gridMain.DataContext = Data.DefaultView;
           
        }

        //Check valid
        private bool KiemTra()
        {

            if (string.IsNullOrEmpty(txtTenXe.Text))
            {
                MessageBox.Show("Chưa vào tên xe!", App.MesseageTitle, MessageBoxButton.OK, MessageBoxImage.Error);
                txtTenXe.Focus();
                return false;
            }
            else
                if (string.IsNullOrEmpty(txtSoHieu.Text) && cm == Command.New)
                {
                    MessageBox.Show("Chưa vào số hiệu xe!", App.MesseageTitle, MessageBoxButton.OK, MessageBoxImage.Error);
                    txtSoHieu.Focus();
                    return false;
                }
                else
                    if (string.IsNullOrEmpty(txtHangSanXuat.Text) && cm == Command.New)
                    {
                        MessageBox.Show("Chưa vào hãng xe!", App.MesseageTitle, MessageBoxButton.OK, MessageBoxImage.Error);
                        txtHangSanXuat.Focus();
                        return false;
                    }
                    else
                        if (string.IsNullOrEmpty(txtSoGhe.Text))
                        {
                            MessageBox.Show("Chưa số lượng ghế!", App.MesseageTitle, MessageBoxButton.OK, MessageBoxImage.Error);
                            txtSoGhe.Focus();
                            return false;
                        }

            return true;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //DataTable tb = ViewModels.TienTeViewModel.GetDmTienTe();
            if (IsEditMode)
            {
                tbMain.Cm_Save += new ucXToolBar.Execute(tbMain_Cm_Save);
                //if(cm == Command.New)
                tbMain.Cm_New += new ucXToolBar.Execute(tbMain_Cm_New);
            }
            //txtMaPhong.IsReadOnly = !(cm == Command.New || cm == Command.Copy);
            txtTenXe.Focus();
            tbMain.Cm_Exit += new ucXToolBar.Execute(tbMain_Cm_Exit);
            //cbbTang.IsEnabled = cm == Command.New ? true : false;
        }

        //Đóng form
        public void tbMain_Cm_Exit()
        {
            this.Close();
        }

        //Tạo mới
        public void tbMain_Cm_New()
        {
            Data = XeAccess.CreateNewXe();
            gridMain.DataContext = Data.DefaultView;
            txtTenXe.Focus();
        }

        //Thêm
        private void tbMain_Cm_Save()
        {
            //Thêm vào CSDL dữ liệu
            if (KiemTra())
            {
                //Data.Rows[0]["password"] = Models.DataProviderModel.MaHoaMatKhau(txtPassword.Password);
                if (XeAccess.ThemXe(Data)>0)
                {
                    this.UpdateSucces = true;
                    if (cm == Command.New)
                        MessageBox.Show("Thêm xe thành công!", App.MesseageTitle, MessageBoxButton.OK, MessageBoxImage.Information);
                    if (cm == Command.Edit)
                        MessageBox.Show("Cập nhật người dùng thành công!", App.MesseageTitle, MessageBoxButton.OK, MessageBoxImage.Information);

                }
            }
        }
    }
}
